//
//  AXFFeebackController.h
//  AXFEightGroup
//
//  Created by hgj on 2016/12/28.
//  Copyright © 2016年 ya. All rights reserved.
//

#import "AXFOrderDetailViewController.h"
#import "AXFOrderViewModel.h"
#import "AXFStatusModel.h"
#import "AXFOrderStateCell.h"
#import "AXFOrderCellViewModel.h"
#import "AXFOrderIDCell.h"
#import "MBProgressHUD+Add.h"
#import "AXFFeebackController.h"

static NSString *stateCellId = @"stateCellId";

static NSString *orderIDCellID = @"orderIDCellID";


@interface AXFOrderDetailViewController ()<UITableViewDelegate,UITableViewDataSource>

@property(nonatomic,weak)UITableView *stateTableView;

@property(nonatomic,weak)UITableView *detailTableView;

@property(nonatomic,weak)UISegmentedControl *segment;

@property (nonatomic, weak) UIView *bottomView;

@property (nonatomic, strong) AXFFeebackController *feedbackVC;

@end

@implementation AXFOrderDetailViewController

- (void)viewDidLoad {
    
    [super viewDidLoad];
    
    self.edgesForExtendedLayout = UIRectEdgeNone;
    
    [self setupNavUI];
    
    [self setupBottomView];
    
    [self setupOrderStateUI];
}

#pragma mark - 底部删除订单 -
- (void)setupBottomView {
    
    //创建
    UIView *bottomView = [[UIView alloc] init];
    bottomView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:bottomView];
    
    UIButton *btn = [[UIButton alloc] init];
    [btn setBackgroundImage:[UIImage imageNamed:@"v2_coupon_verify_normal"] forState:UIControlStateNormal];
    [btn setTitle:@"删除订单" forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(deleteOrder:) forControlEvents:UIControlEventTouchUpInside];
    [bottomView addSubview:btn];
    
    UIView *line = [[UIView alloc] init];
    line.backgroundColor = [UIColor colorWithWhite:0.9 alpha:1];
    [bottomView addSubview:line];
    
    //约束
    [bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(50);
        make.left.bottom.right.mas_equalTo(self.view);
    }];
    
    [btn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(bottomView);
        make.trailing.mas_equalTo(bottomView).offset(-20);
        make.width.mas_equalTo(100);
        make.height.mas_equalTo(35);
    }];
    
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(1);
        make.leading.trailing.equalTo(bottomView);
        make.top.mas_equalTo(bottomView);
    }];
    
    _bottomView = bottomView;
}

- (void)deleteOrder:(UIButton *)button {
    NSLog(@"删除订单");
    [MBProgressHUD showSuccess:@"删除成功" toView:self.view];
}

#pragma mark - 搭建导航栏界面
- (void)setupNavUI {
    
    UISegmentedControl *segment = [[UISegmentedControl alloc]initWithItems:@[@"订单状态",@"订单详情"]];
    segment.selectedSegmentIndex = 0;
    
    // 设置宽度
    for (int i = 0; i < segment.numberOfSegments; i++) {
        [segment setWidth:80 forSegmentAtIndex:i];
    }
    
    //背景黄色
    segment.tintColor = [UIColor orangeColor];
    
    //字体颜色
    [segment setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor blackColor]} forState:UIControlStateSelected];
    
     [segment setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor grayColor]} forState:UIControlStateNormal];
    
    _segment = segment;
    
    self.navigationItem.rightBarButtonItem= [[UIBarButtonItem alloc]initWithTitle:@"投诉" style:UIBarButtonItemStylePlain target:self action:@selector(lodge:)];
    
    [segment addTarget:self action:@selector(changeVc:) forControlEvents:UIControlEventValueChanged];
    
    self.navigationItem.titleView = segment;
}

#pragma mark - 投诉 -
- (void)lodge:(UIBarButtonItem *)item {
    AXFFeebackController *feedbackVC = [AXFFeebackController new];
    feedbackVC.navigationItem.title = @"投诉商家";
    feedbackVC.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"关闭" style:UIBarButtonItemStylePlain target:self action:@selector(dismiss:)];
    _feedbackVC = feedbackVC;
    UINavigationController *vc = [[UINavigationController alloc] initWithRootViewController:feedbackVC];
    [self showDetailViewController:vc sender:nil];
}

- (void)dismiss:(UIBarButtonItem *)item {
    [self dismissViewControllerAnimated:YES completion:nil];
}

#pragma mark - 切换视图
- (void)changeVc:(UISegmentedControl*)segment {
    
    switch (segment.selectedSegmentIndex) {
        case 0:
            [self.detailTableView removeFromSuperview];
            [self setupOrderStateUI];
            break;
        case 1:
            [self.stateTableView removeFromSuperview];
            [self setupOrderDetailUI];
            break;
        default:
            break;
    }
}

#pragma Mark - 订单状态界面
- (void)setupOrderStateUI
{
    UITableView *stateTableView = [[UITableView alloc]init];
    
    _stateTableView = stateTableView;
    
    [self.view addSubview:stateTableView];
    
    [stateTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.mas_topLayoutGuideBottom);
        make.leading.trailing.equalTo(self.view);
        make.bottom.mas_equalTo(_bottomView.mas_top);
    }];
    stateTableView.delegate = self;
    
    stateTableView.dataSource = self;
    

    [stateTableView registerNib:[UINib nibWithNibName:@"AXFOrderStateCell" bundle:nil] forCellReuseIdentifier:stateCellId];
    
    stateTableView.rowHeight = 88;
    //使tableview不能点击只能滑动
    stateTableView.allowsSelection = NO;
    
    stateTableView.tableFooterView = [UIView new];
    stateTableView.separatorInset = UIEdgeInsetsMake(0, 100, 0, 0);
}

#pragma Mark - 订单详情界面
- (void)setupOrderDetailUI {
    
    UITableView *detailTableView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStyleGrouped];
    
    _detailTableView = detailTableView;
    
    [self.view addSubview:detailTableView];
    
    [detailTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.mas_topLayoutGuideBottom);
        make.leading.trailing.equalTo(self.view);
        make.bottom.mas_equalTo(_bottomView.mas_top);
    }];
    detailTableView.delegate = self;
    
    detailTableView.dataSource = self;
    
    [detailTableView registerClass:[AXFOrderIDCell class] forCellReuseIdentifier:orderIDCellID];
    
    //使tableview不能点击只能滑动
    detailTableView.allowsSelection = NO;
    //detailTableView.separatorInset = UIEdgeInsetsMake(0, 200, 0, 0);
    
}

#pragma mark - UITableViewDataSource -

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    if (self.segment.selectedSegmentIndex == 0) {
        
        return 1;
    }
    return 4;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (self.segment.selectedSegmentIndex == 0) {
        
        return self.statusModelList.count;
    }
    
    switch (section) {
        case 0:
            return 7;
//        case 1:
//            return 3;
//        case 2:
//            return 5;
//        case 3:
//            return 1;

        default:
            return 0;
    }
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    if (self.segment.selectedSegmentIndex == 0) {
        
        AXFOrderStateCell *cell = [tableView dequeueReusableCellWithIdentifier:stateCellId forIndexPath:indexPath];
    
        cell.model = self.statusModelList[indexPath.row];
    
        if (indexPath.row == self.statusModelList.count-1) {
            
        cell.separatorInset = UIEdgeInsetsMake(0, SCREEN_WIDTH, 0, 0);
        }
        return cell;
    }
    
    AXFOrderIDCell *cell = [tableView dequeueReusableCellWithIdentifier:orderIDCellID forIndexPath:indexPath];
    switch (indexPath.section) {
        case 0: {
            cell.textLabel.text = self.detailModel.strArr[indexPath.row];
            //cell.separatorInset = UIEdgeInsetsMake(0, SCREEN_WIDTH, 0, 0);
            return cell;
        }
//        case 1: {
//            cell.textLabel.text = self.detailModel.strArr[indexPath.row];
//            return cell;
//        }
//        case 2: {
//            cell.textLabel.text = self.detailModel.strArr[indexPath.row];
//            return cell;
//        }
//        case 3: {
//            cell.textLabel.text = self.detailModel.strArr[indexPath.row];
//            return cell;
//        }
        default:
            return nil;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
}


- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    if (self.segment == 0) {
        return 0;
    }
    return 10;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (self.segment == 0) {
        return 0;
    }
    return 0.1;
}


@end
